package concurrency;

/**
 * @Author DRevelation
 * @Date 2020-06-12 上午 9:05
 * @Desc 单例模式的多线程实现
 * link https://mp.weixin.qq.com/s/DoU52XCt1kjpdl-Q8vI9dw
 */
public class Singleton {

    /**
     * 1.保证变量可见性
     * 2.禁止指令重排序
     */
    private static volatile Singleton singleton;
    private Singleton() {}

    private static Singleton getInstance() {
        if (singleton == null) {
            synchronized (Singleton.class) {
                if (singleton == null) {
                    singleton = new Singleton();
                }
            }
        }
        return singleton;
    }

}
